package com.c1202m.gui;

import com.c1202m.dao.ChuyenKhoanDAO;
import com.c1202m.dao.DangNhapDAO;
import com.c1202m.dao.BangGiaoDichDAO;
import com.c1202m.util.KiemTraDuLieu;
import com.c1202m.dao.TaiKhoanDAO;
import com.c1202m.hibernate.entities.TaiKhoan;
import java.awt.Image;
import javax.swing.text.JTextComponent;

public class ChuyenKhoan extends javax.swing.JDialog {

    private String selectedSoTK;

    public ChuyenKhoan(java.awt.Frame parent, boolean modal, String selectedID) {
        super(parent, modal);
        initComponents();
        Image icon = getToolkit().getImage(getClass().getResource("/images/app_icon_32.png"));
        setIconImage(icon);
        setLocationRelativeTo(DangNhapDAO.as);
        setVisible(true);
        this.selectedSoTK = selectedID;
        fillFields();
    }

    private void fillFields() {
        TaiKhoan danhSachTK = BangGiaoDichDAO.layMotDoiTuong(selectedSoTK);
        txtSoTKGui.setText(String.valueOf(danhSachTK.getSoTK()));
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtSoTKGui = new javax.swing.JTextField();
        txtSoTKNhan = new javax.swing.JTextField();
        txtNguoiNhan = new javax.swing.JTextField();
        txtSoTienChuyen = new javax.swing.JTextField();
        btnUpdate = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        chkXacNhan = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Sua Quan Tri");
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setPreferredSize(new java.awt.Dimension(405, 300));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/lebel_chuyenkhoan.png"))); // NOI18N
        jLabel1.setPreferredSize(new java.awt.Dimension(405, 100));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(51, 51, 51));
        jLabel2.setText("Số tài khoản gửi");
        jLabel2.setPreferredSize(new java.awt.Dimension(100, 20));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(51, 51, 51));
        jLabel3.setText("Số tài khoản nhận");
        jLabel3.setPreferredSize(new java.awt.Dimension(100, 20));

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(51, 51, 51));
        jLabel4.setText("Số tiền chuyển");
        jLabel4.setPreferredSize(new java.awt.Dimension(100, 20));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(51, 51, 51));
        jLabel5.setText("Người nhận");
        jLabel5.setPreferredSize(new java.awt.Dimension(100, 20));

        txtSoTKGui.setEditable(false);
        txtSoTKGui.setBackground(new java.awt.Color(255, 255, 255));
        txtSoTKGui.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txtSoTKGui.setForeground(new java.awt.Color(255, 51, 0));
        txtSoTKGui.setPreferredSize(new java.awt.Dimension(180, 20));

        txtSoTKNhan.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txtSoTKNhan.setForeground(new java.awt.Color(0, 0, 255));
        txtSoTKNhan.setPreferredSize(new java.awt.Dimension(180, 20));
        txtSoTKNhan.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSoTKNhanKeyReleased(evt);
            }
        });

        txtNguoiNhan.setEditable(false);
        txtNguoiNhan.setBackground(new java.awt.Color(255, 255, 255));
        txtNguoiNhan.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        txtNguoiNhan.setForeground(new java.awt.Color(0, 0, 255));
        txtNguoiNhan.setPreferredSize(new java.awt.Dimension(180, 20));

        txtSoTienChuyen.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        txtSoTienChuyen.setPreferredSize(new java.awt.Dimension(180, 20));

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/chuyenkhoan.png"))); // NOI18N
        btnUpdate.setText("Chuyển");
        btnUpdate.setEnabled(false);
        btnUpdate.setMargin(new java.awt.Insets(2, 10, 2, 10));
        btnUpdate.setPreferredSize(new java.awt.Dimension(75, 30));
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit.png"))); // NOI18N
        btnExit.setText("Hủy");
        btnExit.setPreferredSize(new java.awt.Dimension(75, 30));
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        chkXacNhan.setBackground(new java.awt.Color(255, 255, 255));
        chkXacNhan.setText("Xác nhận");
        chkXacNhan.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        chkXacNhan.setEnabled(false);
        chkXacNhan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkXacNhanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(85, 85, 85)
                .addComponent(btnUpdate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(85, 85, 85)
                .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(15, 15, 15)
                        .addComponent(txtNguoiNhan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(chkXacNhan))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(15, 15, 15)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtSoTKGui, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtSoTKNhan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtSoTienChuyen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSoTKGui, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSoTKNhan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNguoiNhan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chkXacNhan))
                .addGap(7, 7, 7)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSoTienChuyen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        dispose();
    }//GEN-LAST:event_btnExitActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        JTextComponent[] tcp = {txtSoTKGui, txtSoTienChuyen};
        if (!KiemTraDuLieu.kiemTraTruongTrong(tcp)) {
            return;
        }
        String soTKNhan = null;
        if (!KiemTraDuLieu.isEmpty(txtSoTKNhan.getText())) {
            soTKNhan = String.valueOf(txtSoTKNhan.getText());
        }
        double soTienChuyen = 0;
        if (!KiemTraDuLieu.isEmpty(txtSoTienChuyen.getText())) {
            if (!KiemTraDuLieu.kiemTraSo(txtSoTienChuyen)) {
                return;
            } else {
                soTienChuyen = Double.valueOf(txtSoTienChuyen.getText());
            }
        }
        if (soTienChuyen < Help.guiToiThieu) {
            new CustomMessageDialog(null, true, "Warning!", "<html>Gửi tiền thất bại<br/>Số tiền chuyển không hợp lệ</html>", CustomMessageDialog.MESSAGE);
            return;
        }
        if (ChuyenKhoanDAO.chuyenKhoan(selectedSoTK, soTKNhan, soTienChuyen)) {
            new CustomMessageDialog(null, true, "Done!", "Chuyển khoản thành công!", CustomMessageDialog.DONE);
            BangGiaoDich.lamMoiBangDanhSachTK();
            BangGiaoDich.lamMoiBangChuyenKhoan();
            BangTaiKhoan.lamMoiBangTK();
            dispose();
        }
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void txtSoTKNhanKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSoTKNhanKeyReleased
        if (TaiKhoanDAO.layMotDoiTuong(txtSoTKNhan.getText()) != null) {
            txtNguoiNhan.setText(TaiKhoanDAO.layMotDoiTuong(txtSoTKNhan.getText()).getChuTK());
            chkXacNhan.setEnabled(true);
        } else {
            txtNguoiNhan.setText("Tài khoản không tồn tại");
        }
    }//GEN-LAST:event_txtSoTKNhanKeyReleased

    private void chkXacNhanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkXacNhanActionPerformed
        // TODO add your handling code here:
        if (chkXacNhan.isSelected()) {
            btnUpdate.setEnabled(true);
            txtSoTKNhan.setEditable(false);
        } else {
            btnUpdate.setEnabled(false);
            txtSoTKNhan.setEditable(true);
        }
    }//GEN-LAST:event_chkXacNhanActionPerformed
    //</editor-fold>
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JCheckBox chkXacNhan;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtNguoiNhan;
    private javax.swing.JTextField txtSoTKGui;
    private javax.swing.JTextField txtSoTKNhan;
    private javax.swing.JTextField txtSoTienChuyen;
    // End of variables declaration//GEN-END:variables
}
